package proto;

import java.util.List;
import java.util.ArrayList;

public class Oscilloscope implements BuildingBlock {

	private List<Boolean> memory;
	private Input input;
	private final String name;
	
	public String getName() {
		return name;
	}

	public Oscilloscope(String name) {
		super();
		this.memory = new ArrayList<Boolean>();
		this.input = new Input(0);
		this.name = name;
		
		if(Main.verbose){
			System.out.println("added block name: " + name + " type: SCOPE");
		}
	}
	
	@Override
	public Boolean processSignal() {
		
		Boolean changeOccured = false;
		Boolean in = this.input.getState();
		
		if(!this.memory.isEmpty() && !this.memory.get(this.memory.size()-1).equals(in)){
			changeOccured = true;
		}
		
		this.memory.add(in);
		if(Main.verbose){
			System.out.println("set block " + this.getName() + " to: " + (in ? "1" : "0"));
		}
		return changeOccured;
	}

	@Override
	public Boolean validate() {
		return (this.input.getConnectedOutput() != null);
	}

	public List<Boolean> getMemory() {
		return memory;
	}

	public void setMemory(List<Boolean> memory) {
		this.memory = memory;
		
		if(this.memory == null){
			this.memory = new ArrayList<Boolean>();
		}
	}

	public Input getInput() {
		return input;
	}

	public void setInput(Input input) {
		this.input = input;
		
		if(this.input == null){
			this.input = new Input(0);
		}
	}

}
